# frozen_string_literal: true

module Gitlab
  module VulnerabilityScanning
    class Advisory
      attr_reader :xid, :title, :description, :solution, :identifiers, :urls, :affected_component, :cvss_v2, :cvss_v3

      # rubocop:disable Metrics/ParameterLists
      # Creates a new advisory object that can be used to create findings
      # via the Gitlab::VulnerabilityScanning::FindingBuilder classes.
      #
      # @param xid [String] The external id of the advisory,
      #   e.g. this would be the uuid for advisories sourced from the GitLab Advisory Database.
      # @param title [String] The title of the advisory,
      #   e.g. Allocation of File Descriptors or Handles Without Limits or Throttling.
      # @param description [String] The description of the advisory.
      # @param solution [String] The solution of the advisory.
      # @param identifiers [Array<String>] A list of identifiers for the advisory, e.g. ["CVE-2023-00001"].
      # @param urls [Array<String>] A list of URLs with resources related to the advisory, e.g. ["https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2023-00001"].
      # @param affected_component [Gitlab::VulnerabilityScanning::AffectedComponent]
      #   The component affected by the advisory.
      # @param cvss_v3 [String, CvssSuite::Cvss3, CvssSuite::Cvss31, nil] A cvss vector string, CvssSuite::Cvss3 object,
      #   or CvssSuite::Cvss31 object.
      # @param cvss_v2 [String, CvssSuite::Cvss2, nil] A cvss vector string or CvssSuite::Cvss2 object.
      def initialize(
        xid:, title:, description:, solution:, identifiers:, urls:, affected_component:, cvss_v3: nil,
        cvss_v2: nil)
        @xid = xid
        @title = title
        @description = description
        @solution = solution
        @identifiers = identifiers
        @urls = urls
        @affected_component = affected_component
        @cvss_v3 = init_cvss_v3(cvss_v3)
        @cvss_v2 = init_cvss_v2(cvss_v2)
      end
      # rubocop:enable Metrics/ParameterLists

      private

      def init_cvss_v3(cvss)
        return if cvss.nil?

        return cvss if cvss.instance_of?(CvssSuite::Cvss3) || cvss.instance_of?(CvssSuite::Cvss31)

        CvssSuite.new(cvss)
      end

      def init_cvss_v2(cvss)
        return if cvss.nil?

        return cvss if cvss.instance_of?(CvssSuite::Cvss2)

        CvssSuite.new(cvss)
      end
    end
  end
end
